package vn.team.clipxonxao.adapter;

import java.util.List;

import vn.team.clipxonxao.model.Category;
import vn.team.clipxonxao.ui.R;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.mk.fw.imageloader.MKImageFetcher;

public class SlidingMenuAdapter extends BaseAdapter {

	private FragmentActivity mContext;
	private List<Category> listCategory;
	private LayoutInflater inflater;
	private ViewHolder viewHolder;
	private MKImageFetcher imageLoader;

	public SlidingMenuAdapter(FragmentActivity mContext,
			List<Category> listCategory, MKImageFetcher imageLoader) {
		this.mContext = mContext;
		this.listCategory = listCategory;
		this.inflater = LayoutInflater.from(this.mContext);
		this.imageLoader = imageLoader;
	}

	@Override
	public int getCount() {
		return listCategory.size();
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		viewHolder = new ViewHolder();
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.layout_slidingmenu_items,null, false);
			viewHolder.avatarMenu = (ImageView) convertView.findViewById(R.id.layout_slidingmenu_items_image);
			viewHolder.txtNameTitle = (TextView) convertView.findViewById(R.id.layout_slidingmenu_items_title);
			convertView.setTag(viewHolder);
		}else{
			viewHolder = (ViewHolder)convertView.getTag();
		}
		try {
			Category category = listCategory.get(position);
			imageLoader.getImageFetcher().loadImage(category.getThumnail(), viewHolder.avatarMenu);
			viewHolder.txtNameTitle.setText(""+category.getName());
		} catch (Exception e) {
			e.printStackTrace();
		}

		return convertView;
	}
	
	private class ViewHolder{
		public ImageView avatarMenu;
		public TextView txtNameTitle;
	}

}
